package com.xiaogao.serviceEdu.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaogao.commonUtils.JwtUtils;
import com.xiaogao.commonUtils.Result;
import com.xiaogao.commonUtils.orderVo.CourseInfoOrder;
import com.xiaogao.serviceEdu.client.OrderClient;
import com.xiaogao.serviceEdu.entity.EduCourse;
import com.xiaogao.serviceEdu.entity.vo.ChapterVo;
import com.xiaogao.serviceEdu.entity.vo.CourseInfoVO;
import com.xiaogao.serviceEdu.entity.vo.CourseQuery;
import com.xiaogao.serviceEdu.entity.vo.CourseWebVo;
import com.xiaogao.serviceEdu.service.EduChapterService;
import com.xiaogao.serviceEdu.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author 小高
 * @createtime 2021-06-30-14:50
 * @description
 */
@RestController
@RequestMapping("/serviceEdu/courseFront")
@Api(description = "前台课程管理页")
public class CourseFrontController {

    @Autowired
    private EduCourseService courseService;
    
    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OrderClient orderClient;


    @ApiOperation(value = "分页条件查询课程")
    @PostMapping("/getCourseFrontList/{page}/{limit}")
    public Result getCourseFrontList(
            @ApiParam(name = "page",value = "当前页",required = true)
            @PathVariable("page")Long page,

            @ApiParam(name = "limit",value = "每页显示数",required = true)
            @PathVariable("limit")Long limit,

            @ApiParam(name = "courseQuery",value = "查询对象",required = false)
            @RequestBody(required = false) CourseQuery courseQuery){

        Page<EduCourse> pageCourse = new Page<>(page,limit);
        Map<String,Object> map = courseService.getPageList(pageCourse,courseQuery);
        return Result.ok().data(map);
    }

    @ApiOperation(value = "根据课程id查询课程的详细信息")
    @GetMapping("/getCourseInfoWebById/{courseId}")
    public Result getCourseInfoWebById(
            @ApiParam(name = "courseId",value = "课程id",required = true)
            @PathVariable("courseId")String courseId, HttpServletRequest request){
        // 查询课程信息和讲师信息
        CourseWebVo courseWebVo = courseService.selectInfoWebById(courseId);
        // 查询当前课程的章节信息
        List<ChapterVo> chapterVoList = chapterService.getChapterVideoByCourseId(courseId);

        // 远程调用，判断课程是否被购买
        boolean buyCourse = orderClient.isBuyCourse(JwtUtils.getMemberIdByJwtToken(request), courseId);

        return Result.ok().data("course",courseWebVo).data("chapterVoList",chapterVoList).data("isbuy",buyCourse);
    }

    // 根据课程id获取课程信息
    @PostMapping("/getCourseInfoOrder/{courseId}")
    public CourseInfoOrder getCourseInfoOrder(@PathVariable("courseId") String courseId){
        CourseInfoVO courseInfo = courseService.getCourseInfoVoById(courseId);
        CourseInfoOrder courseInfoOrder = new CourseInfoOrder();
        BeanUtils.copyProperties(courseInfo,courseInfoOrder);
        return courseInfoOrder;
    }
}
